package com.hadron.ui.form.comboBox 
{
	import components.api.IComboBox;
	import components.events.ComboBoxEvent;
	import org.casalib.collection.List;
	
	[Event(name = "comboOpened", type = "com.hadron.ui.form.comboBox.ComboBoxEvent")]
	[Event(name = "comboClosed", type = "com.hadron.ui.form.comboBox.ComboBoxEvent")]
	[Event(name = "comboSelected", type = "com.hadron.ui.form.comboBox.ComboBoxEvent")]
	/**
	 * ...
	 * @author Hadron
	 */
	public class AbstractComboBox extends AbstractBase implements IComboBox
	{
		protected var _items:List = new List();
		protected var _selectedIndex:int = -1;
		protected var _selectedItem:Object;
		protected var _selectedLabel:String;
		protected var _defaultText:String;
		public var isEnabled:Boolean = true;
		public var isOpened:Boolean = false;
		
		public function AbstractComboBox() 
		{
			super();
		}
		
		/* INTERFACE components.api.IComboBox */
		
		public function addItem(item:Object):void
		{
			_items.addItem(item);
		}
		
		public function addItemAt(item:Object, index:uint):void
		{
			_items.addItemAt(item, index);
		}
		
		public function removeItem(item:Object):void
		{
			_items.removeItem(item);
		}
		
		public function removeItemAt(index:uint):void
		{
			_items.removeItemAt(index);
		}
		
		public function getItemAt(index:uint):Object
		{
			return _items.getItemAt(index);
		}
		
		public function set items(value:List):void
		{
			if (value) _items = value;
		}
		
		public function get items():List
		{
			return _items;
		}
		
		public function set defaultText(value:String):void
		{
			if (value) _defaultText = value;
		}
		
		public function get defaultText():String
		{
			return _defaultText;
		}
		
		public function open():void	
		{	
			
		}
		
		public function close():void 
		{
			
		}
		
		public function enable():void
		{
			isEnabled = true;
			mouseChildren = true;
			mouseEnabled = true;
			validateNow();
		}
		
		public function disable():void
		{
			isEnabled = false;
			mouseChildren = false;
			mouseEnabled = false;
		}
		
		public function get length():uint
		{
			return _items.size;
		}
		
		public function get selectedIndex():int
		{
			return _selectedIndex;
		}
		
		public function set selectedItem(item:Object):void
		{
			if (containsObj(item) && _selectedItem != item)
			{
				_selectedItem = item;
				_selectedIndex = _items.indexOf(_selectedItem);
				dispatchEvent(new ComboBoxEvent(ComboBoxEvent.COMBO_SELECTED));
			}
		}
		
		public function get selectedItem():Object
		{
			return _selectedItem;
		}
		
		public function set selectedLabel(value:String):void
		{
			//TODO: implement method
		}
		
		public function get selectedLabel():String
		{
			return (_selectedItem) ? _selectedItem.label : null;
		}
		
		/**
		 * Redraws component regarding current items.size
		 */
		public function validateNow():void
		{
			
		}
		
		public function indexOf(obj:Object):int
		{	
			var i:int;
			for (i = 0; i < _items.size; ++i)
			{
				if (_items.getItemAt(i) == obj)
				{
					return i;
				}
			}
			return -1;
		}
		
		public function containsObj(obj:Object):Boolean
		{
			return indexOf(obj)>-1;
		}
	}
}